<?php
/*
*form này khách hàng sử dụng 
*/
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('frmmanagercoffee_loadMenu');      
$objXajax->registerFunction('frmmanagercoffee_loadGrid');      
$objXajax->registerFunction('frmmanagercoffee_checkedTable');

$objXajax->registerFunction('frmmanagercoffee_addRemoveProduct');
$objXajax->registerFunction('frmmanagercoffee_moveTable');
$objXajax->registerFunction('frmmanagercoffee_loadProduct');
$objXajax->registerFunction('frmmanagercoffee_finishTable');

$objXajax->registerFunction('frmmanagercoffee_addNewProduct');
$objXajax->registerFunction('frmmanagercoffee_ajxTotalTable');
$objXajax->registerFunction('frmmanagercoffee_undo');
$objXajax->registerFunction('frmmanagercoffee_loadTable');


$arrDetail;
/*
* Hàm dùng để load sản phâm cho left menu 
* 
*/
function frmmanagercoffee_loadMenu(){     

    global $lang,$arrMenu;
    $objResponse = new xajaxResponse();
    $arr = array();
    $strMenu = frmmanagercoffee_getMenu($arr);    
    $objResponse->addAssign('frmmanagercoffee_leftmenu', 'innerHTML', $strMenu);
    //$objResponse->addScriptCall("frmmanagercoffee_dropdrapmenu();");  
    $objResponse->addScriptCall("addJsContext_coffee();");  
    
    $count = count($arr);
    for($i=0;$i<$count;$i++){ // tạo mãng tên và giá cho sản phẩm
        $name     = $arr[$i]['name'];
        $price    = $arr[$i]['price'];
        $id       = $arr[$i]['id'];
        $unit     = $arr[$i]['unit'];
        $unitname = $arr[$i]['unitname'];
        $objResponse->addScript("cf_obj.createAssignProductPrice($id,'$name','$price','$unit','$unitname');"); 
    }
    $indexNewProduct = time();
    $objResponse->addScript("cf_obj.indexNewProduct=$indexNewProduct;");                            
                                 
    return $objResponse->getXML();
}
function frmmanagercoffee_loadTable(){ 
    global $lang,$arrDetail;
        $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
        $arrDetail = frmmanagercoffee_getTableInit();
        for($i=0;$i<count($arrDetail);$i++){
            $id = $arrDetail[$i]['id'];
            $name = $arrDetail[$i]['name'];
            $type = $arrDetail[$i]['type_id'];
            $abc.=$id.';';
            $objResponse->addScript("cf_obj.createAssignTableObj('$id','$name','$type');");         

        }
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function frmmanagercoffee_loadGrid(){     

    global $lang,$arrMenu,$arrDetail,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrDetail = array();
    $arrTable = frmmanagercoffee_getTable($arrDetail);
    

    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_managercoffee_grid.html');
   
    $oTbs->MergeBlock('blk_total', $arrTable); 
    $oTbs->MergeBlock('blk_data', 'array', 'arrDetail[%p1%]'); 
    

    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmmanagercoffee_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("addJsContext();");  
            
   $objResponse->addScript('$(".clsbtn_coffee").button();');          
   $objResponse->addScript('xajax_frmmanagercoffee_loadMenu();');          
   $objResponse->addScript('xajax_frmmanagercoffee_loadProduct();');          
        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  frmmanagercoffee_getTable(&$arrDetail){
    global $objDbSelect,$tbl_postfix;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.room_type,r.name,t.color,t.name  as name_type,t.id as type_id
    FROM coffee_table$tbl_postfix as r join coffee_room_type$tbl_postfix  as t on r.room_type=t.id  order by r.room_type,r.name";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    for($i=0;$i<count($arr);$i++){
        $type_id = $arr[$i]['type_id'];
        $arrResult[$type_id]= array('type_id'=>$arr[$i]['type_id'],'name_type'=>$arr[$i]['name_type']);
        $arrDetail[$type_id][]=array('id'=>$arr[$i]['id'],'color'=>$arr[$i]['color'],'name'=>$arr[$i]['name']);
    } 
    return $arrResult;
}
/*
*hàm này dùng để lấy danh sách bảng lên để lưu vào mãng 
* 
*/
function  frmmanagercoffee_getTableInit(){
    global $objDbSelect,$hotel_id,$tbl_postfix;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.room_type,r.name,t.color,t.name  as name_type,t.id as type_id
    FROM coffee_table$tbl_postfix as r join coffee_room_type$tbl_postfix  as t on r.room_type=t.id order by r.room_type,r.name";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    return $arr;
}
    

function frmmanagercoffee_getMenu(&$arr){
    global $objDbSelect,$lang,$tbl_postfix,$hotel_id;
    $sql = "select p1.name_$lang as name,p1.price as price,p1.unit,p1.unitname,p1.id,p1.product_type ,p2.name_$lang as parentname ,p2.icon
        from tbl_product_new$tbl_postfix as p1 join tbl_product_new$tbl_postfix as p2 on p1.parent_id
    = p2.id and p2.active = 1 where  p1.active = 1 order by p1.parent_id,p1.product_type " ;
    
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $arrResult = array();
    $index =-1;  
    $strresult='';
    $first = true;

    for($i=0;$i<$count;$i++){
        $name = $arr[$i]['name'];
        $price = $arr[$i]['price'];
        if($arr[$i]['product_type']==0){
            if(!$first){
             $strresult .= "</div>" ;             
            } 
            $strresult.="<div><span>$name</span>" ; 
            $first = false;
        }else{
            $id = $arr[$i]['id'];
           $strresult.="<a href='#' id='frmmanagercoffee_liProd_$id'>$name</a>" ;         
        }
    }
    
    $strresult = '<div style="float: left" id="my_menu" class="sdmenu">'.$strresult."</div></div>" ;
    //echo $strresult; die();
    return $strresult;
}
                                           
function frmmanagercoffee_addRemoveProduct($arrAddRemove,$arrProduct=''){
     global $lang,$arrMenu,$objDbSelect;
     $objResponse = new xajaxResponse();
    // $arrId= explode(';',$i);
     for($i=0;$i<count($arrAddRemove);$i++){
         $temp = $arrAddRemove[$i];
         $arrId = explode(';',$temp);
         $product_id = $arrId[0];
         $nump = $arrId[2];
         $table_id = $arrId[1];
         frmmanagercoffee_actionDB($table_id,$product_id,$nump,$arrProduct) ;
     }    
    
     return $objResponse->getXML();    
}

function frmmanagercoffee_actionDB($table_id,$product_id,$nump,$arrProduct){
  global $objDbSelect,$user_id,$hotel_id,$tbl_postfix;

       $sqlSelect = "select num from coffee_history_product$tbl_postfix where table_id =  
     '$table_id' and product_id = '$product_id' and flg = 0 ";
     $arrSelect = $objDbSelect->GetArray($sqlSelect);
     $sql ='';
     if(is_array($arrSelect) && count($arrSelect)==1){
         $curnum =  $arrSelect[0]['num'];// sớ sản phẩm đã có hiện tại
         $nextnum = $curnum + $nump;
         if($nextnum<1){
             $sql= "delete from coffee_history_product$tbl_postfix where table_id =  
     '$table_id' and product_id = '$product_id' and flg = 0 ";     
     // write log........................
         }else{
            $sql = "update coffee_history_product$tbl_postfix SET num = '$nextnum' where table_id =  
     '$table_id' and product_id = '$product_id' and flg = 0 ";     
      // write log........................ 
         }
     }else{
         $name = $arrProduct[$product_id]['name'];
         $price = $arrProduct[$product_id]['price'];
       $sql = "insert into coffee_history_product$tbl_postfix(table_id,product_id,name,price,date_created,user_created,num,hotel_id) 
       values('$table_id','$product_id','$name','$price',now(),'$user_id',$nump,'$hotel_id')"; 
       // write log........................   
     }    
     $objDbSelect->Execute($sql);  
}

//function frmmanagercoffee_moveTable($arrMove){
//   global $objDbSelect;
//   for($i=0;$i<count($arrMove);$i++){
//       $temp  = $arrMove[$i]; 
//       $arrId = explode(';',$temp);
//       $preTable = $arrId[0];
//       $curTable = $arrId[1];
//       $sql = "update tbl_current_book_room set room_id = $curTable , flg_move = 1 where room_id = $preTable";
//       $objDbSelect->Execute($sql); 
//   }
   // write log........................
//   $objResponse = new xajaxResponse(); 
//   return $objResponse->getXML();    
//}

function frmmanagercoffee_moveTable($tableFrom,$tableTo){
    global $objDbSelect,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $sql = "update coffee_history_product$tbl_postfix set table_id = $tableTo where table_id  = $tableFrom and flg = 0";
    $objDbSelect->Execute($sql);     
    return $objResponse->getXML();  
}
/*
* Hàm này dùng để load product lên các bàn 
* 
* 
*/
function frmmanagercoffee_loadProduct(){
    global $objDbSelect,$tbl_postfix,$hotel_id;
     $objResponse = new xajaxResponse();
    $sql = "select * from coffee_history_product$tbl_postfix where flg = 0 order by table_id"; // lấy các sản phẩm có trên bàn từ DB
    $arr = $objDbSelect->GetArray($sql);
    $objResponse->addScript("frmmanagercoffee_dropdrapmenu();");
    for($i=0;$i<count($arr);$i++){
        $table_id = $arr[$i]['table_id'];
        $name = $arr[$i]['name'];
        $num = $arr[$i]['num'];
        $price = $arr[$i]['price'];
        $product_id = $arr[$i]['product_id'];
        $objResponse->addScript("frmmanagercoffee_loadProduct('$table_id','$product_id','$name','$num','$price');"); // load các sản phẩm đó lên bàn

    }
  //  
    $objResponse->addScript("calActiveTableByTableType();");
   // 
    
   return $objResponse->getXML();  
}
/*
* hàm này dùng để tính toán khi khách hàn kêu tính tiền  
* @table_id : id của bàn cần tính 
* 
*/
function frmmanagercoffee_finishTable($table_id,$totalPrice=0){
    
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $general_id = time();
    $sql = " update coffee_history_product$tbl_postfix SET general_id=$general_id,flg =1 where flg=0 and table_id = $table_id";
    $objDbSelect->Execute($sql);
    $sqlInsert = "insert into coffee_total$tbl_postfix(table_id,total,general_id,date_created,user_created) value($table_id,$totalPrice,$general_id,now(),$user_id)";
    $objDbSelect->Execute($sqlInsert);
    return $objResponse->getXML();
}
/*
* Hàm này dùng để add sản phẩm ngoài doanh mục 
* $proId : lấy id từ indexproduct
* $hdtTable : id của bàn thêm sản phẩm mới
* $name : tên sản phẩm 
* $price : giá của sản phẩm ngoài doanh mục
*/
function frmmanagercoffee_addNewProduct($proId,$hdTable,$name,$price){
    global $objDbSelect,$user_id,$hotel_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $sql =''; 
    $sql = "insert into coffee_history_product$tbl_postfix(product_id,name,price,date_created,user_created,num,hotel_id,table_id) 
    values('$proId','$name','$price',now(),'$user_id',1,'$hotel_id','$hdTable')";  
     $objDbSelect->Execute($sql);  
    return $objResponse->getXML();    
}

function frmmanagercoffee_undo($table_id){
    global $objDbSelect,$tbl_postfix,$hotel_id;
    $objResponse = new xajaxResponse();
    $sqlgeneral = "select max(general_id) as general_id from coffee_history_product$tbl_postfix where table_id=$table_id";
    $arrgeneral = $objDbSelect->GetArray($sqlgeneral);
    $general_id = $arrgeneral[0]['general_id'];
    $sql = "select * from coffee_history_product$tbl_postfix where flg = 1 and general_id = $general_id"; // lấy các sản phẩm có trên bàn từ DB

    $arr = $objDbSelect->GetArray($sql);
    for($i=0;$i<count($arr);$i++){
        $table_id = $arr[$i]['table_id'];
        $name = $arr[$i]['name'];
        $num = $arr[$i]['num'];
        $price = $arr[$i]['price'];
        $product_id = $arr[$i]['product_id'];
        $objResponse->addScript("frmmanagercoffee_loadProduct('$table_id','$product_id','$name','$num','$price');"); // load các sản phẩm đó lên bàn

    }
    $sqlupdate = "update coffee_history_product$tbl_postfix set flg =0 where general_id = $general_id";
    $objDbSelect->Execute($sqlupdate);
    $sqldelete = " delete from tbl_total$tbl_postfix where general_id = $general_id ";// xóa dữ liệu trong bản đã tính toán
    $objDbSelect->Execute($sqldelete);
    return $objResponse->getXML();    
}
    
?>
